//
//  LRTabBar.swift
//  LRWeibo
//
//  Created by Ruihaha on 16/9/22.
//  Copyright © 2016年 macbook air . All rights reserved.
//

import UIKit

class LRTabBar: UITabBar {

    //定义一个闭包
    var composeClickClosure:(()->())?
    
    lazy var composeBtn : UIButton={
        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        btn.sizeToFit()
        
        return btn
    }()
    
    
    override init(frame: CGRect) {
       super.init(frame: frame)
        addSubview(composeBtn)
        composeBtn.addTarget(self, action: #selector(composeBtnDidClick), for: .touchUpInside)
    }
    
    //系统帮助添加 禁止从xib加载视图
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //连接OC方法 添加点击事件
    @objc private func composeBtnDidClick(){
        composeClickClosure?()
    }
        
    override func layoutSubviews() {
        
        //MARK: 错位 忘记写此方法没有背景 没有点击事件
        super.layoutSubviews()
        let btnW = UIScreen.main.bounds.width/5
        let btnH = self.bounds.height
        var index = 0
        for subview in subviews{
            print(subview)
            //之前没写这句 会导致btn错位
            if subview .isKind(of: NSClassFromString("UITabBarButton")!){
            
            subview.frame = CGRect(x: CGFloat(index)*btnW, y: 0, width: btnW, height: btnH)
            (index != 1) ? (index+=1):(index+=2)
            }
        }

        composeBtn.center = CGPoint(x: UIScreen.main.bounds.width*0.5, y: self.bounds.size.height*0.5)

        
    }
    
}
